<?php

/**
 * @author      Nguyen Duc Thuan <me@ndthuan.com>
 * @copyright   Copyright (c) 2009 Nguyen Duc Thuan <me@ndthuan.com>
 */
final class Fete_Registry
{
    static private $_instance = null;

    private $_data;

    /**
     * @return Fete_Registry
     */
    static public function &getInstance()
    {
        if (null === self::$_instance) {
            self::$_instance = new self();
        }

        return self::$_instance;
    }

    /**
     * @return Fete_Registry
     */
    public function &set($key, $value = null)
    {
        if (is_array($key)) {
            foreach ($key as $name => $value)
            {
                $this->_data[$name] = $value;
            }
        } else {
            $this->_data[$key] = $value;
        }

        return $this;
    }

    public function get($key, $defaultValue = null)
    {
        if (!isset($this->_data[$key])) {
            $this->_data[$key] = $defaultValue;
        }
        return $this->_data[$key];
    }

    /**
     * @return Fete_Registry
     */
    public function &__set($name, $value)
    {
        $this->_data[$name] = $value;

        return $this;
    }

    public function __get($key)
    {
        return $this->get($key);
    }

    public function __isset($key)
    {
        return isset($this->_data[$key]);
    }
}